package follows

import (
	"github.com/gin-gonic/gin"
	"savor_gin/controllers"
	. "savor_gin/errorType"
	"savor_gin/models"
	"strconv"
	"time"
)

// 添加收藏
func AddFollows(c *gin.Context) {
	var follow models.Follows
	if err := c.ShouldBindJSON(&follow); err != nil {
		controllers.SendResp(c, ParamBindError, nil)
		return
	}
	follow.FollowDate = time.Now()
	if err := follow.Create(); err != nil {
		controllers.SendResp(c, DataBaseError, nil)
		return
	}
	controllers.SendResp(c, OK, follow)
}

// 删除收藏
func DeleteFollows(c *gin.Context) {
	followId, err := strconv.Atoi(c.Query("followId"))
	if err != nil {
		controllers.SendResp(c, ParamBindError, err)
		return
	}
	s := &models.Follows{FollowID: uint64(followId)}
	if err := s.Delete(); err != nil {
		controllers.SendResp(c, DataBaseError, err)
		return
	}
	controllers.SendResp(c, OK, nil)
}

// 查询收藏列表
func GetFollowsListByUserId(c *gin.Context) {
	userId, err := strconv.Atoi(c.Query("userId"))
	if err != nil {
		controllers.SendResp(c, ParamBindError, err)
		return
	}
	if s, err := models.GetFollow(uint64(userId)); err != nil {
		controllers.SendResp(c, DataBaseError, err)
		return
	} else {
		controllers.SendResp(c, OK, s)
	}
}
